<?php
namespace app\api\controller;

use app\api\validate\User as UserValidate;

class User extends Common
{
    protected $checkLoginExclude = ['login'];

    public function login()
    {
        $data = [
            'name' => \json_decode($this->request->post('name/s', '', 'trim')),
            'password' => \json_decode($this->request->post('password/s', ''))
        ];
        $validate = new UserValidate;
        if (!$validate->check($data)) {
            $this->error('登录失败：' . $validate->getError() . '。');
        }
        $result = $this->auth->login($data['name'], $data['password']);
        if (!$result) {
            $this->error('登录失败：' . $this->auth->getError() . '。');
        }
        $this->success('登录成功。', null, $result);
    }

    public function userinfo()
    {
        $user = $this->auth->getLoginUser();
        if (!$this->auth->isTimeOut()) {
            $this->success('', null, [
                'id' => $user->id,
                'name' => $user->username,
                'role' => $user->admin_role_id
            ]);
        }
    }
}
